home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-12-06 | 4.8 KB | 316 lines | [TEXT/KAHL] |
- /***
- *
- * CStream.cp - I/O stream classes BBEdit extensions.
- * Copyright © 1995 by Christopher E. Hyde. All rights reserved.
- *
- ***/
-
- #include "Exceptions.h"
- #include "BBEdit.h"
- #include "CStream.h"
-
-
- // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- // Class CStream
- // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
-
- #if 0
- CStream::CStream (void)
- {
- fHandle = nil;
- }
- #endif
-
-
- void
- CStream::Open (Handle aHandle, bool direction, bool ownsHandle)
- {
- FailNil(fHandle = aHandle);
- fDirection = direction;
- fOwnsHandle = ownsHandle;
-
- HLockHi(aHandle);
- fBase = (uchar*) *aHandle;
- fLength = InlineGetHandleSize(aHandle);
- fOffset = 0;
- }
-
-
- void
- CStream::SetLength (UInt32 length)
- {
- HUnlock(fHandle);
- SetHandleSize(fHandle, length);
- FailMemError();
- HLockHi(fHandle);
-
- fBase = (uchar*) *fHandle;
- fLength = length;
- if (fOffset > length)
- fOffset = length;
- }
-
-
- Handle
- CStream::GetHandle (void)
- {
- SetLength(fOffset);
- return fHandle;
- }
-
-
- void
- CStream::Flush (void)
- {
- if (fOffset > 0) {
- BBEdit->Insert(Ptr(fBase), fOffset);
- fOffset = 0;
- }
- }
-
-
- void
- CStream::Close (void)
- {
- #if 0
- if (fHandle) {
- if (IsOutput()) { // If it's and output stream then it owns the Handle
- Flush();
- DisposeHandle(fHandle);
- fHandle = nil;
- } else
- HUnlock(fHandle);
- }
- #else
- if (fHandle) {
- if (IsOutput()) // If it's an output stream then flush it
- Flush();
- if (OwnsHandle())
- DisposeHandle(fHandle);
- else
- HUnlock(fHandle);
- fHandle = nil;
- fLength = 0;
- }
- #endif
- }
-
-
- // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- // Class CIStream
- // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
-
- #if 0
- CIStream::End (void)
- {
- if (fHandle)
- HUnlock(fHandle);
- }
- #endif
-
-
- void
- CIStream::Open (WindowPtr window)
- {
- CStream::Open(BBEdit->GetWindowContents(window), kInputStream, !kOwnsHandle);
- }
-
-
- // Initialise the input stream to the current selection
- void
- CIStream::Open (WindowPtr window, long selStart, long selEnd)
- {
- Open(window);
- fOffset = selStart;
- fLength = selEnd;
- }
-
-
- int
- CIStream::Get (void)
- {
- return (fOffset < fLength) ? fBase[fOffset++] : EOF;
- }
-
-
- // Returns a line in DST if the line length < SIZE, otherwise fails
- // If this is the last line then a CR is appended!
-
- void
- CIStream::Get (char* dst, int size)
- {
- #if 1
- char c;
- --size; // Subtract 1 for the CR
- while ((c = Get()) != EOF && c != '\x0D' && --size > 0)
- *dst++ = c;
- *dst++ = '\x0D';
- *dst = '\0';
- #elif 0
- while (fOffset < fLength && --size > 0) {
- char c = fBase[fOffset++];
- *dst++ = c;
- if (c == '\x0D')
- break;
- }
- *dst = '\0';
- // return size;
-
- #else
-
- const char* src = &fBase[fOffset];
- if (size > fLength - fOffset)
- size = fLength - fOffset;
- const char* max = &src[size - 2];
-
- while (src < max) {
- char c = src++;
- *dst++ = c;
- if (c == '\x0D')
- goto gotCR;
- }
- if (src < &fBase[fLength])
- Fail(errEOF);
- *dst++ = '\x0D';
- gotCR:
- *dst = '\0';
- fOffset = src - fBase;
- #endif
- }
-
-
- int
- CIStream::CurChar (void) const
- {
- return (fOffset < fLength) ? fBase[fOffset] : EOF;
- }
-
-
- #if 0
- bool
- CIStream::IsEOF (void) const
- {
- return fOffset < fLength;
- }
-
-
- void
- CIStream::Close (void)
- {
- if (fHandle)
- HUnlock(fHandle);
- }
- #endif
-
-
- // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- // Class COStream
- // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
-
- #if 0
- COStream::~COStream (void)
- {
- if (fHandle)
- DisposeHandle(fHandle);
- }
- #endif
-
-
- void
- COStream::Open (bool autoFlush, UInt32 length)
- {
- fAutoFlush = autoFlush;
- CStream::Open(NewHandle(length), kOutputStream, kOwnsHandle);
- }
-
-
- void
- COStream::Put (char ch)
- {
- if (fOffset >= fLength)
- Flush();
-
- fBase[fOffset++] = ch;
-
- if (fAutoFlush)
- Flush();
- }
-
-
- void
- COStream::Put (const char* ptr, UInt32 length)
- {
- #if 0
- if (fOffset + length > fLength)
- SetLength(fLength + Max(kGrowStream, length));
-
- BlockMoveData(ptr, fBase + fOffset, length);
- fOffset += length;
- #else
- if (fOffset + length > fLength)
- Flush();
-
- for (UInt32 i = fLength - fOffset; length > i; i = fLength - fOffset) {
- BlockMoveData(ptr, fBase + fOffset, i);
- fOffset += i;
- ptr += i;
- length -= i;
- Flush();
- }
-
- BlockMoveData(ptr, fBase + fOffset, length);
- fOffset += length;
- #endif
- if (fAutoFlush)
- Flush();
- }
-
-
- void
- COStream::Put (ConstStr255Param aString)
- {
- Put((const char*) &aString[1], StrLength(aString));
- }
-
-
- void
- COStream::Put (const char* str)
- {
- Put(str, strlen(str));
- }
-
-
- void
- COStream::NewLine (void)
- {
- Put('\x0D');
- }
-
-
- #if 0
- Handle
- COStream::GetHandle (void)
- {
- SetLength(fOffset);
- return fHandle;
- }
-
-
- void
- COStream::Flush (void)
- {
- BBEdit->Paste(GetHandle());
- fOffset = 0;
- }
-
-
- void
- COStream::Close (void)
- {
- if (fHandle) {
- DisposeHandle(fHandle);
- fHandle = nil;
- }
- }
- #endif
-